package com.shopp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopp.auto.JSONAuthentication;
import com.shopp.entity.CarFood;
import com.shopp.entity.ShFood;
import com.shopp.entity.ShOrder;
import com.shopp.entity.ShUser;
import com.shopp.mapper.CarFoodMapper;
import com.shopp.mapper.ShFoodMapper;
import com.shopp.mapper.ShUserMapper;
import com.shopp.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopp
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/car-food")
public class CarFoodController extends JSONAuthentication {
    @Autowired
    CarFoodMapper carFoodMapper;
    @Autowired
    ShUserMapper shUserMapper;
    @Autowired
    ShFoodMapper shFoodMapper;
    @ApiOperation(value = "向购物车添加商品")
    @PostMapping("/add")
    public void addcar(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("foodId") int foodId) throws Exception {
        CarFood carFood=new CarFood();
        carFood.setUserId(userId);
        carFood.setFoodId(foodId);
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        wrapper.eq("food_id",foodId);
        if(carFoodMapper.selectOne(wrapper)!=null){
            Result error = Result.error().message("此商品你已加入购物车");
            this.WriteJSON(request, response, error);
        }else{
            if (carFoodMapper.insert(carFood)> 0) {
                Result ok = Result.ok().message("添加成功");
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }

    }
    @ApiOperation(value = "删除购物车的商品")
    @PostMapping("/del")
    public void delZiLiao(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = carFoodMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改购物车商品的数量")
    @PostMapping("/updProNum")
    public void updProNum(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id, @RequestParam("num") int num) throws Exception {

      CarFood carFood= carFoodMapper.selectById(id);
      carFood.setNum(num);
        if (carFoodMapper.updateById(carFood) > 0) {
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询购物车的商品")
    @PostMapping("/findFoodByUserId")
    public void findFoodByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size, @RequestParam("current") int current,@RequestParam("userId") int userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
         wrapper.eq("user_id",userId);
        Page<CarFood> page=new Page<>(current,size);
        Page<CarFood> pages=carFoodMapper.selectPage(page,wrapper);
        List<CarFood> records = pages.getRecords();
        for(CarFood a:records){
            a.setShUser(shUserMapper.selectById(a.getUserId()));
            a.setShFood(shFoodMapper.selectById(a.getFoodId()));
        }
        int total=carFoodMapper.selectCount(wrapper);
        if (records.size() > 0) {
            Result ok = Result.ok().data("foods",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品");
            this.WriteJSON(request, response, error);
        }
    }
}

